﻿
using Leadyo.WebService;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace Leadyo.WebService
{
    public abstract class WebDalSQL<T> : WebAbsDAL<T>, IBaseDAL<T> where T : class ,new()
    {
         private static readonly Hashtable objCache = new Hashtable();
           private static readonly object syncRoot = new Object();
           public WebDalSQL()
		    {
                Init();
           }
           
           ///// <summary>
           ///// 数据新增
           ///// </summary>
           ///// <param name="obj"></param>
           ///// <returns></returns>
           //public virtual int Insert2(object obj)
           //{
           //    string strFiled = "";
           //    string filedValue = "";
           //    string sql = "";
           //    System.Reflection.PropertyInfo[] properties = obj.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
           //    foreach (System.Reflection.PropertyInfo item in properties)
           //    {
           //        if (outoKeyValue != item.Name)
           //        {
           //            string name = item.Name;
           //            object value = item.GetValue(obj, null);
           //             strFiled += item.Name + ",";
           //             if (item.GetValue(obj, null) == null)
           //                 filedValue += "NULL" + ",";
           //             else
           //                 filedValue += "'" + item.GetValue(obj, null) + "'" + ",";

           //        }
           //    }
           //    strFiled = strFiled.Trim(',');
           //    filedValue = filedValue.Trim(',');
           //    sql = " INSERT INTO " + tableName + "( " + strFiled + " ) Values ( " + filedValue + " ) ";
           //    return base.ExecuteNonQuery(sql);

           //}
        ///// <summary>
        ///// 获取最新一条数据
        ///// </summary>
        ///// <returns></returns>
        //   public T GetFirstEntriy()
        //   {
        //       string sql = "";
        //       sql = string.Format(" SELECT top 1 * FROM {0} ORDER BY {1}", tableName,sortIndex);//获取最前一条数据
        //       return DataReaderToEntity(BaseBuildSql.GetModel(sql));
        //   }
        //   /// <summary>
        //   /// 获取最新一条数据
        //   /// </summary>
        //   /// <returns></returns>
        //   public T GetLastEntriy()
        //   {
        //       string sql = "";
        //       sql = string.Format(" SELECT top 1 * FROM {0} ORDER BY {1} DESC", tableName, sortIndex);//获取最前一条数据
        //       return DataReaderToEntity(BaseBuildSql.GetModel(sql));
        //   }
          
       }

       

        //public virtual bool UpDate(object obj)
        //{

        //}

       /// <summary>
       /// Author by lzw
       /// Create Date:2017-04-14
       /// 模块功能：根据业务对象的类型进行反射操作辅助类
       /// </summary>
       /// <typeparam name="T"></typeparam>
       public class Reflect<T> where T : class
       {
           private static readonly Hashtable ObjCache = new Hashtable();
           private static readonly object syncRoot = new Object();

           /// <summary>
           ///     根据参数创建对象实例
           /// </summary>
           /// <param name="sName">对象全局名称</param>
           /// <param name="sFilePath">文件路径</param>
           /// <returns></returns>
           public static T Create(string sName, string sFilePath, Object[] parameters = null)
           {
               return Create(sName, sFilePath, true, parameters);
           }

           /// <summary>
           ///     根据参数创建对象实例
           /// </summary>
           /// <param name="sName">对象全局名称</param>
           /// <param name="sFilePath">文件路径</param>
           /// <param name="bCache">缓存集合</param>
           /// <returns></returns>
           public static T Create(string sName, string sFilePath, bool bCache, object[] parameters = null)
           {
               //string path = Application.StartupPath + @"\\" + sFilePath;
               var CacheKey = sName;
               T objType = null;
               if (bCache)
               {
                   objType = (T)ObjCache[CacheKey]; //从缓存读取 
                   if (!ObjCache.ContainsKey(CacheKey))
                   {
                       lock (syncRoot)
                       {
                           objType = CreateInstance(CacheKey, sFilePath, parameters);
                           ObjCache.Add(CacheKey, objType); //缓存数据访问对象
                       }
                   }
               }
               else
               {
                   objType = CreateInstance(CacheKey, sFilePath, parameters);
               }

               return objType;
           }

           /// <summary>
           ///     根据全名和路径构造对象
           /// </summary>
           /// <param name="sName">对象全名</param>
           /// <param name="sFilePath">程序集路径</param>
           /// <returns></returns>
           private static T CreateInstance(string sName, string sFilePath, object[] parameters = null)
           {
               
               var assemblyObj = Assembly.Load(sFilePath);
               if (assemblyObj == null)
               {
                   throw new ArgumentNullException("sFilePath", string.Format("无法加载sFilePath={0} 的程序集", sFilePath));
               }
               T obj = null;
               if (parameters != null)
               {
                   obj = (T)assemblyObj.CreateInstance(sName, true, BindingFlags.Default, null, parameters, null, null);
               }
               else
               {
                   obj = (T)assemblyObj.CreateInstance(sName, true);
               }
               return obj;
           }
    }
}
